package com.huawei.hwid.core.b.a.a;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import com.huawei.cloudservice.CloudAccountManager;
import com.huawei.cloudservice.CloudRequestHandler;
import com.huawei.hms.support.hianalytics.HiAnalyticsConstant;
import com.huawei.hwid.core.b.a.a.c;
import com.huawei.hwid.core.constants.HwAccountConstants;
import com.huawei.hwid.core.d.b;
import com.huawei.hwid.core.d.b.e;
import com.huawei.hwid.core.d.p;
import com.huawei.hwid.core.helper.handler.ErrorStatus;
import com.huawei.hwid.f.g;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlSerializer;

public class d extends com.huawei.hwid.core.b.a.a {
    private String h;
    private String i;
    private String j;
    private String k;
    private String l;
    private String m;
    private String n;
    private String o;
    private int p;

    static class a extends com.huawei.hwid.core.helper.handler.a {
        private CloudRequestHandler a;

        public a(Context context, CloudRequestHandler cloudRequestHandler) {
            super(context);
            this.a = cloudRequestHandler;
        }

        public void a(Bundle bundle) {
            super.a(bundle);
            bundle.putBoolean(HwAccountConstants.SERVICETOKENAUTH_IS_SECCUSS, true);
            this.a.onFinish(bundle);
        }

        public void b(Bundle bundle) {
            super.b(bundle);
            ErrorStatus errorStatus = (ErrorStatus) bundle.getParcelable("requestError");
            if (errorStatus != null) {
                e.b("QueryTrustCircleStatusRequest", "requestError code = " + errorStatus.getErrorCode() + " msg = " + errorStatus.getErrorReason(), true);
                this.a.onError(errorStatus);
                return;
            }
            this.a.onError(new ErrorStatus(32, "ErrorStatus is null"));
        }
    }

    public d(Context context, String str, String str2, String str3, String str4, int i) {
        this.k = HwAccountConstants.TYPE_TENCENT;
        this.n = "/IUserPwdMng/getTCISAT";
        this.o = this.n;
        this.p = 2;
        this.g = 0;
        a(c.a);
        f(str);
        h(str2);
        g(str3);
        i(str4);
        a(context, i);
        c(this.p);
        a(true);
    }

    public String j() throws IllegalArgumentException, IllegalStateException, IOException {
        OutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        try {
            XmlSerializer a = p.a(byteArrayOutputStream);
            a.startDocument("UTF-8", Boolean.valueOf(true));
            a.startTag(null, "GetTCISATReq");
            p.a(a, "version", "27120");
            p.a(a, "userID", this.h);
            p.a(a, "uuid", this.i);
            p.a(a, "udid", this.j);
            p.a(a, CloudAccountManager.KEY_REQCLIENTTYPE, this.k);
            a.endTag(null, "GetTCISATReq");
            a.endDocument();
            String byteArrayOutputStream2 = byteArrayOutputStream.toString("UTF-8");
            return byteArrayOutputStream2;
        } finally {
            try {
                byteArrayOutputStream.close();
            } catch (IOException e) {
                e.d("QueryTrustCircleStatusRequest", "IOException", true);
            }
        }
    }

    public void a(String str) throws XmlPullParserException, IOException {
        XmlPullParser a = p.a(str.getBytes("UTF-8"));
        for (int eventType = a.getEventType(); 1 != eventType; eventType = a.next()) {
            String name = a.getName();
            switch (eventType) {
                case 2:
                    if (HiAnalyticsConstant.BI_KEY_RESUST.equals(name)) {
                        this.b = a(a);
                    }
                    if (this.b != 0) {
                        if (!"errorCode".equals(name)) {
                            if (!"errorDesc".equals(name)) {
                                break;
                            }
                            this.d = a.nextText();
                            break;
                        }
                        this.c = g.a(a.nextText());
                        break;
                    } else if (!"tcisAT".equals(name)) {
                        if (!"fullyTrustedDev".equals(name)) {
                            break;
                        }
                        this.m = a.nextText();
                        break;
                    } else {
                        this.l = a.nextText();
                        break;
                    }
                default:
                    break;
            }
        }
    }

    private int a(XmlPullParser xmlPullParser) {
        int i = -1;
        Object attributeValue = xmlPullParser.getAttributeValue(null, "resultCode");
        if (!TextUtils.isEmpty(attributeValue)) {
            try {
                i = g.a(attributeValue);
            } catch (NumberFormatException e) {
                e.d("QueryTrustCircleStatusRequest", "getResultCode NumberFormatException", true);
            }
        }
        return i;
    }

    public void f(String str) {
        this.h = str;
    }

    private void g(String str) {
        this.i = str;
    }

    private void h(String str) {
        this.j = str;
    }

    private void i(String str) {
        this.k = str;
    }

    public String b() {
        return this.o;
    }

    public Bundle c() {
        Bundle c = super.c();
        c.putString("tcisAT", this.l);
        c.putString("userID", this.h);
        c.putString("fullyTrustedDev", this.m);
        return c;
    }

    public String c(Context context) {
        return b.f(context);
    }

    public void a(Context context, com.huawei.hwid.core.b.a.a aVar, String str, CloudRequestHandler cloudRequestHandler) {
        com.huawei.hwid.core.b.a.e.a(context, aVar, str, a(context, aVar, new a(context, cloudRequestHandler)));
    }
}
